package yh.thread.threadcreate;

import java.util.concurrent.Callable;

/**
 * 线程创建
 *
 * @author: zyh
 * @date: 2024/10/10 12:16
 **/
public class ThreadCreate {


    /**
     * 方法1 匿名内部类
     */
    public  void MyThreadMethod1() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName() + "正在执行.....");
            }
        }).start();
        // 等价于下面的方法
        new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + "正在执行.....");
        }).start();
    }


    /**
     * 方法2 实现Thread 接口
     */
    public static class MyThreadMethod2 extends Thread {
        @Override
        public void run() {
            String name = Thread.currentThread().getName();
            System.out.println("当前线程名称：" + name);
        }
    }




    /**
     * 方法3 实现runnable 接口
     */
   public static class MyThreadMethod3 implements  Runnable{
        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName() + "正在执行....");
        }
    }


    /**
     * 方法4 实现callable 接口
     */
    public  static  class MyThreadMethod4 implements Callable<Integer>{
        @Override
        public Integer call() throws Exception {
            System.out.println(Thread.currentThread().getName() + "正在执行....");
            return 0;
        }
    }






}
